package com.own.component.device.config;

import com.own.component.device.base.RequestDeviceManager;
import com.own.component.device.impl.DefaultAttributeRequestDeviceManager;
import com.own.component.device.impl.DefaultCookieRequestDeviceManager;
import com.own.component.device.impl.DefaultHeaderRequestDeviceManager;
import com.own.component.device.impl.DefaultMultipleRequestDeviceManager;
import com.own.component.device.impl.DefaultParameterRequestDeviceManager;
import com.own.component.device.property.DeviceProperty;
import com.own.constant.ConstantRequestManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/**
 * DefaultDeviceConfig
 *
 * @author chenxueli
 * @date 2024-03-15 11:32:39
 */
@Slf4j
@SpringBootConfiguration
public class DefaultDeviceConfig {

    @Resource
    private DeviceProperty deviceProperty;

    @Bean
    @ConditionalOnMissingBean(value = RequestDeviceManager.class)
    public RequestDeviceManager requestDeviceManager() {
        return switch (deviceProperty.getFieldSourceType()) {
            case ConstantRequestManager.RequestFieldSourceType.ATTRIBUTE ->
                    new DefaultAttributeRequestDeviceManager(deviceProperty);
            case ConstantRequestManager.RequestFieldSourceType.COOKIE ->
                    new DefaultCookieRequestDeviceManager(deviceProperty);
            case ConstantRequestManager.RequestFieldSourceType.HEADER ->
                    new DefaultHeaderRequestDeviceManager(deviceProperty);
            case ConstantRequestManager.RequestFieldSourceType.MULTIPLE ->
                    new DefaultMultipleRequestDeviceManager(deviceProperty);
            case ConstantRequestManager.RequestFieldSourceType.PARAMETER ->
                    new DefaultParameterRequestDeviceManager(deviceProperty);
            default -> throw new IllegalArgumentException("不支持的请求参数获取来源");
        };
    }

}
